﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client;
using Yugi.Client.Card;

namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for MessageBox.xaml
	/// </summary>
	public partial class ActiveMessageBoxAndChoice : UserControl
	{
        public event Action<object, string> OnChoice;
        private Canvas parent;
        public ActiveMessageBoxAndChoice()
		{
			this.InitializeComponent();
            this.State = MState.Hide;
            Linker.choic_message_box = this;
            this.Loaded += new RoutedEventHandler(ActiveMessageBoxAndChoice_Loaded);
		}

        void ActiveMessageBoxAndChoice_Loaded(object sender, RoutedEventArgs e)
        {
            //this.parent = this.Parent as Canvas;
            //this.parent.Children.Remove(this);
        }
        public void Clear()
        {
            foreach (var item in this.img_stack.Children)
            {
                Image img = item as Image;
                img.MouseEnter -= new MouseEventHandler(img_MouseEnter);
            }
            this.img_stack.Children.Clear();
        }
        public void Add(CardModel source)
        {
            this.AddImage(source.YCard.displayImage).DataContext = source.YCard;
        }
        public void Add(Yugi.Client.Card.YBaseCard source)
        {
            this.AddImage(source.displayImage).DataContext=source;
        }
        Point last = new Point(-999, -999);
       
        void MyMessageBox_MouseMove(object sender, MouseEventArgs e)
        {
            
            Point p = e.GetPosition(this.Parent as UIElement);
          
          
            if (e.LeftButton == MouseButtonState.Pressed)
            {
               
                double left = Canvas.GetLeft(this);
                double top = Canvas.GetTop(this);
                Canvas.SetLeft(this, left + p.X - last.X);
                Canvas.SetTop(this, top + p.Y - last.Y);
                
            }
            last = p;
        }
        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;
            string res=null;
            switch (bt.Content.ToString())
            {
                case "OK":
                    res = "Ok";                 
                    break;
            }
            if (card_choice.Count != this.NumCardChoice)
            {
                MessageBox.Show("Bạn Chua Chon Bai");
                return;
            }
            if (OnChoice != null && res != null)
            {
                OnChoice(this, res);
               
            }
            Canvas.SetZIndex(this, -100);
            this.State = MState.Hide;
            ((Canvas)this.Parent).Background = Brushes.Transparent;
            ((Canvas)this.Parent).Opacity = 1;
            ((Canvas)this.Parent).IsHitTestVisible = false;
            if (Complete != null)
                Complete(card_choice);
            Complete = null;
            //this.parent.Children.Remove(this);
            this.Visibility = Visibility.Collapsed;
        }

        public Image AddImage(ImageSource img_src)
        {
            Image img = new Image();
            img.Source = img_src;
            img.Width = 60;
            img.Height = 100;
            img.Margin = new Thickness(2, 5, 0, 0);
            img.DataContext = this.img_stack.Children.Count;
            this.img_stack.Children.Add(img);
            img.MouseEnter += new MouseEventHandler(img_MouseEnter);
            return img;
        }

        void img_MouseEnter(object sender, MouseEventArgs e)
        {
            Image img = sender as Image;
            Linker.MainWindow.s.ReceiveMessageFromChildren(ClientMSG.PrintCard, ((Yugi.Client.Card.YBaseCard)img.DataContext));
        }
        Action<object> Complete=null;
        public void BeginChoice(Action<object> complete)
        {
            this.Complete = complete;
            card_choice.Clear();
            foreach (Image item in this.img_stack.Children)
            {
                item.MouseLeftButtonDown += new MouseButtonEventHandler(item_MouseLeftButtonDown);
            }
            this.ShowMsg(String.Format("Chon {0} lá bài trong danh sách sau:",this.NumCardChoice));
        }
        private void EndChoice()
        {
            foreach (Image item in this.img_stack.Children)
            {
                item.MouseLeftButtonDown -= new MouseButtonEventHandler(item_MouseLeftButtonDown);
            }
           
        }
        void item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
            Image img = sender as Image;
            int id = ((YBaseCard)img.DataContext).ID;
            if (!card_choice.Contains(id))
            {
                img.Margin = new Thickness(2, -20, 0, 0);
                card_choice.Add(id);
                if (card_choice.Count == NumCardChoice)
                    EndChoice();
            }
            else
            {
                img.Margin = new Thickness(2, 5, 0, 0);
                card_choice.Remove(id);
            }

        }

        private List<int> card_choice = new List<int>();
        public int NumCardChoice { get; set; }

        bool allow_drag = false;
        public bool AllowDrag
        {
            get
            {
                return allow_drag;
            }
            set
            {
                allow_drag = value;
                if (this.Parent is Canvas)
                {
                    if (value == true)
                        this.MouseMove += new MouseEventHandler(MyMessageBox_MouseMove);
                    else
                        this.MouseMove -= new MouseEventHandler(MyMessageBox_MouseMove);
                }
            }
        }
        public string Message
        {
            get
            {
                return this.message.Text;
            }
            set
            {
                this.message.Text = value;
            }
        }
        private MState current;
        public MState State
        {
            get
            {
                return current;
            }
            set
            {
                current = value;
                VisualStateManager.GoToState(this, current.ToString(), true);
               
            }
        }
        public void ShowMsg(string text)
        {
            //this.parent.Children.Add(this);
            Canvas.SetZIndex(this, 100);
            this.message.Text = text;
            this.Visibility = Visibility.Visible;
            this.State = MState.Show;
            ((Canvas)this.Parent).Background = Brushes.Black;
            ((Canvas)this.Parent).Opacity = 0.7;
            ((Canvas)this.Parent).IsHitTestVisible = true;
        }
        public enum MState
        {
            Show,Hide
        }
	}
}